<?php
namespace app\common\repositories\apps;


use app\common\repositories\BaseRepository;
use app\common\dao\apps\AppsWebDao;
use app\common\model\apps\AppsWebModel;
use think\facade\Db;

class AppsWebRepository extends BaseRepository
{
    protected $dao;
    public function __construct(){
        $this->dao = new AppsWebDao();
    }

    /**
     * @desc: create one AppsAccess
     * @param varchar $appkey 
     * @param varchar $device 
     * @param varchar $custom 
     * @author: generate
     */
    public function createOneAppsWeb(
        $appkey,
        $device = '',
        $custom = '',
        $fingerprint = '',
        $version = ''
    ) {
        
        // to do  some check
        $data = compact('appkey','custom','device','fingerprint','version');

        $data['is_del'] = 0;
        $data['create_at'] =  date('Y-m-d H:i:s');

        $id = Db::transaction(function () use($data) {
            $id = $this->create($data);
            // todo other

            return $id;
        });

        return ['code' => 200, 'msg' => 'ok', 'data' => ['id' => $id]];
    }
    /**
     * @desc: get first custom
     * @author: xiaohp
     * @param {*} $appkey
     * @param {*} $fingerprint
     */
    public function getFirstCustom($appkey,$fingerprint){
        $where = ['appkey_key' => $appkey,'fingerprint_key' => $fingerprint,'is_del' => 0,'status'];
        $info = $this->dao->search($where)->order('id ASC')->find();
        $return = [];
        if(!empty($info)){
            $custom = $info->custom ?? '';
            if(!empty($custom)){
                $custom = trim($custom);
                if(!empty($custom) && is_string($custom)){
                    parse_str($custom, $return);
                }
            }
        }
        return $return;
    }
    /**
     * @desc: statistics
     * @author: xiaohp
     * @param {*} $app_key
     */
    public function getAppStatistics($app_key){
        $where = ['appkey_key' => $app_key,'is_del' => 0,'status'=>1];
        $ios_count = $this->dao->search($where)->where('device->isIOS','true')->count();
        $android_count = $this->dao->search($where)->where('device->isAndroid','true')->count();
        $repet_count = $this->dao->search($where)->group('fingerprint')->count();
        $today_count = $this->dao->search($where)->where('created_at','>',date('Y-m-d') . ' 00:00:01')->count();
        
        $list = [];
        $list[] = [
            'title' => 'iOS',
            'value' => $ios_count
        ];
        $list[] = [
            'title' => 'Android',
            'value' => $android_count
        ];
        $list[] = [
            'title' => '去重',
            'value' => $repet_count
        ];
        $list[] = [
            'title' => '今日新增',
            'value' => $today_count
        ];
        return $list;
    }


    /**
     * @desc: statistics line
     * @author: xiaohp
     * @param {*} $app_key
     */
    public function getAppStatisticsLine($app_key){

        // 获取最近七天的 每天开始时间 结束时间
        $date_list = [];
        $xdata = [];
        for ($i = 6; $i >= 0; $i--) {
            $date = date('Y-m-d', strtotime('-' . $i . ' days'));
            $date_list[] = [
                'start' => $date . ' 00:00:00',
                'end' => $date . ' 23:59:59',
            ];
            $xdata[] = $date;
        }

        // 查询每天的 ios  安卓数
        $ios_list = [];
        $android_list = [];
        $ios_repeat_list = [];
        $android_repeat_list = [];
        foreach ($date_list as $key => $item) {
            $where = ['appkey_key' => $app_key,'is_del' => 0,'status'=>1];
            $ios_count = $this->dao->search($where)->where('device->isIOS','true')->whereBetween('created_at',[$item['start'],$item['end']])->count();
            $android_count = $this->dao->search($where)->where('device->isAndroid','true')->whereBetween('created_at',[$item['start'],$item['end']])->count();

            $ios_repeat_count = $this->dao->search($where)->where('device->isIOS','true')->group('fingerprint')->whereBetween('created_at',[$item['start'],$item['end']])->count();
            $android_repeat_count = $this->dao->search($where)->where('device->isAndroid','true')->group('fingerprint')->whereBetween('created_at',[$item['start'],$item['end']])->count();

            $ios_list[] = $ios_count;
            $android_list[] = $android_count;
            $ios_repeat_list[] = $ios_repeat_count;
            $android_repeat_list[] = $android_repeat_count;
        }
        $series = [];
        $series[] = [
            'name' => 'ios',
            'type' => 'line',
            'stack' => 'Total',
            'data' => $ios_list,
        ];
        $series[] = [
            'name' => 'android',
            'type' => 'line',
            'stack' => 'Total',
            'data' => $android_list,
        ];
        $series[] = [
            'name' => 'iosrepeat',
            'type' => 'line',
            'stack' => 'Total',
            'data' => $ios_repeat_list,
        ];
        $series[] = [
            'name' => 'androidrepeat',
            'type' => 'line',
            'stack' => 'Total',
            'data' => $android_repeat_list,
        ];
        $legend = ['data' => ['ios', 'android', 'iosrepeat', 'androidrepeat']];
        $xAxis = ['data' => $xdata, 'boundaryGap' => false,'type' => 'category'];
        $result = [
            'series' => $series,
            'legend' => $legend,
            'xAxis' => $xAxis,
        ];
        return $result;
    }
}